<template>
	<view class="login">
		<view class="title">
			欢迎来到图书商城!
		</view>
		<view class="form">
			<u-form :model="ruleForm" ref="ruleForm">
				<u-form-item label="邮箱" prop="email">
					<u-input v-model="ruleForm.email" placeholder="请输入邮箱" />
				</u-form-item>
				<u-form-item label="密码" prop="password">
					<u-input v-model="ruleForm.password" placeholder="请输入密码" type="password"/>
				</u-form-item>
			</u-form>
			<u-button @click="submit" type="primary" class='btn'>登录</u-button>
		</view>
		<view class="footer u-flex u-row-between">
			<view>
				找回密码
			</view>
			<navigator url="/pages/auth/register/register">
				注册
			</navigator>
		</view>
	</view>
</template>

<script>
	import {
		getInfo,
		getUser
	} from '../../../common/http.api.js';
	export default {
		data() {
			return {
				ruleForm: {
					email: '',
					password: '',
				},
				rules: {
					email: [{
						required: true,
						message: '请输入邮箱',
						// 可以单个或者同时写两个触发验证方式 
						trigger: 'blur',
					}, {
						validator: (rule, value, callback) => {
							return this.$u.test.email(value);
						},
						message: '邮箱格式错误',
						trigger: ['blur'],
					}],
					password: [{
						required: true,
						message: '请输入密码',
						trigger: 'blur'
					}, {
						min: 6,
						message: '密码至少为6位',
						trigger: 'blur'
					}, ]
				}
			}
		},
		methods: {
			submit() {
				this.$refs.ruleForm.validate(valid => {
					uni.showLoading({
						title: '加载中'
					});
					if (valid) {
						console.log(111)
						this.$u.api.getInfo(this.ruleForm).then(res => {
							console.log(res)
							this.$store.commit("changeLoginStatus", res.data);
							uni.hideLoading()
							// 获取用户详情
							this.$u.api.getUser().then(res=>{
								this.$store.commit("changeUser",res.data);
								let currentRoute='/'+uni.getStorageSync('currentRoute') || "/pages/home/home";
								setTimeout(() => {
									uni.reLaunch({
										url: currentRoute
									})
								}, 1500)
							})
						})
					}else{
						uni.hideLoading()
					}

				});
			}
		},
		onReady() {
			this.$refs.ruleForm.setRules(this.rules);
		}
	};
</script>

<style lang="scss" scoped>
	.login {
		padding: 0 80rpx;

		.title {
			font-size: 50rpx;
			font-weight: bold;
			height: 200rpx;
			line-height: 200rpx;
			text-align: center;
		}

		.btn {
			margin-top: 30rpx;
		}

		.footer {
			color: #808080;
			margin-top: 10rpx;
		}
	}
</style>
